package aufgabe1_mit_threads;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client extends Thread{

	public Client(){
		setDaemon(true);
	}
	
	@SuppressWarnings("unused")
	public void run(){
		Socket server1 = null;
		try {
		server1 = new Socket("localhost", 10002);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Socket server1 = null;

		try {
			// Zugriff 1
			server1 = new Socket("localhost", 10002);
			Scanner in1 = new Scanner(server1.getInputStream());
			PrintWriter out1 = new PrintWriter(server1.getOutputStream(), true);

			InputStreamReader eingabestream = new InputStreamReader(System.in);
			BufferedReader lese = new BufferedReader(eingabestream);
			
			boolean client_run = true;
			
			while(client_run){
				String input = lese.readLine();
				
				// Sende an Server
				out1.println(input);
				
				// Bekomme Antwort
				String output = in1.nextLine();
				System.out.println(output);
				if (input.equals("BYE") &&  output.equals("BYE") || input.equals("SHUTDOWN root") &&  output.equals("OK")){
					out1.println(input);
					client_run = false;
				}
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				server1.close();
				System.out.println("Console: Client Socket disconnect");
			} catch (IOException e) {
			}
		}
	}

}
